package edu.nf.meituan.dao.user.impl;

import edu.nf.meituan.dao.user.UserDAO;
import edu.nf.meituan.model.user.User;
import edu.nf.util.DBUtils;
import org.nf.db.util.SqlExecutor;
import org.nf.db.util.result.BeanHandler;

/**
 * @Date 2023-06-19
 * @Author qiu
 */
public class UserDaoImpl implements UserDAO {

    /**
     * 用户登录
     * @param username
     * @return
     */
    @Override
    public User loginUser(String username) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());

        String sql = "select u_name,pwd,nickname,u_phone,u_avatar from users where u_name = ?";

        BeanHandler<User> userBeanHandler = new BeanHandler<>(User.class);

        return sqlExecutor.executeQuery(sql, userBeanHandler, username);
    }

    /**
     * 用户注册
     * @param uName
     * @param pwd
     * @param nickName
     * @param uPhone
     * @return
     */
    @Override
    public int registerUser(String uName, String pwd, String nickName, String uPhone) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "insert into users(u_name,pwd,nickname,u_phone) values(?,?,?,?)";

        return sqlExecutor.executeUpdate(sql, uName, pwd, nickName, uPhone);
    }

    /**
     * 查询手机号是否注册
     * @param phone
     * @return
     */
    @Override
    public User equeryUserPhone(String phone) {

        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());

        String sql = "select * from users where u_phone = ?";

        BeanHandler<User> userBeanHandler = new BeanHandler<>(User.class);

        return sqlExecutor.executeQuery(sql, userBeanHandler, phone);
    }
}
